//
//  MenuTopView.m
//  AllYoga
//
//  Created by liwei on 2017/5/31.
//  Copyright © 2017年 biyunkeji. All rights reserved.
//

#import "MenuTopView.h"
#import "MenuLayout.h"
#import "MenuCell.h"

#define YGRGB(r,g,b,a) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a]
//屏幕宽度比例
#define kXX [UIScreen mainScreen].bounds.size.width / 375
//屏幕高度比例
#define kYY [UIScreen mainScreen].bounds.size.height / 667
#define YGWeak __weak typeof(self) weakSelf = self


@interface MenuTopView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;

//左边视图
@property (nonatomic,strong) UIView *menuView;

//左边菜单栏按钮
@property (nonatomic,strong) UIButton *menuBtn;

//记录当前展示的item
@property (nonatomic,strong) UIButton *currentItem;

//标签宽度
@property (nonatomic,assign) CGFloat itemWidth;

//记录当前选中的标签
@property (nonatomic,strong) NSIndexPath *currentIndexPath;


@end

@implementation MenuTopView

- (UIView *)menuView {

    if (!_menuView) {
        _menuView = [[UIView alloc] init];
        _menuView.layer.shadowColor = YGRGB(213, 204, 204, 0.8).CGColor;
        _menuView.layer.shadowOffset = CGSizeMake(4, 0);
        _menuView.layer.shadowOpacity = 0.8;
        _menuView.layer.backgroundColor = [UIColor whiteColor].CGColor;
        [self addSubview:_menuView];    }
    return _menuView;
}


- (UIButton *)menuBtn {

    if (!_menuBtn) {
        _menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_menuBtn setImage:[UIImage imageNamed:@"recommend_menu"] forState:UIControlStateNormal];
        [_menuBtn addTarget:self action:@selector(didClickMenuBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.menuView addSubview:_menuBtn];
    }
    return _menuBtn;
}



- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self loadSubViews];
        self.layer.shadowOpacity = 0.6;
        self.layer.shadowOffset = CGSizeMake(0, 4);
        self.layer.shadowColor = YGRGB(213, 204, 204, 0.6).CGColor;
        self.layer.backgroundColor = [UIColor clearColor].CGColor;
        
        self.normalFont = [UIFont systemFontOfSize:12];
        self.selectedFont = [UIFont systemFontOfSize:15];
        self.normalColor = [UIColor lightGrayColor];
        self.selectedColor = [UIColor darkGrayColor];
        
        [self updateSubViewsFrame];
        
    }
    return self;
}

- (void)loadSubViews {
    
    MenuLayout *layout = [[MenuLayout alloc] init];
    layout.itemSize = CGSizeMake((self.frame.size.width - self.frame.size.height) / 3, self.frame.size.height);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height) collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.layer.backgroundColor = [UIColor whiteColor].CGColor;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.decelerationRate = 0;
    self.collectionView = collectionView;
    collectionView.layer.masksToBounds = NO;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MenuCell class]) bundle:nil]  forCellWithReuseIdentifier:@"MenuCell"];
    [self addSubview:collectionView];
    
    self.itemWidth = layout.itemSize.width;
    
    
}


- (void)updateSubViewsFrame {

    self.menuView.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    
    self.menuBtn.center = CGPointMake(self.menuView.frame.size.width * 0.5, self.menuView.frame.size.height * 0.5);
    self.menuBtn.bounds = CGRectMake(0, 0, 20 * kYY, 20 * kYY);
    
    [self bringSubviewToFront:self.menuView];
}

- (void)setDataSource:(NSMutableArray *)dataSource {

    _dataSource = dataSource;

    self.currentIndexPath = nil;
    [self.collectionView setContentOffset:CGPointMake(0, 0)];
    [self.collectionView reloadData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    });
}

#pragma mark - <UICollectonViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.tagTitle = self.dataSource[indexPath.item];
    
    if (indexPath == self.currentIndexPath) {
        cell.selectedFont = [UIFont systemFontOfSize:15];
    }else {
        cell.normalFont = [UIFont systemFontOfSize:12];
    }
    
    return cell;
}
#pragma mark - <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [self updateScrollOffsetWith:(int)indexPath.item];
    [self triggerTopItemRequestWith:(int)indexPath.item];
    
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
    [self updateScrollOffsetWith:(scrollView.contentOffset.x / self.itemWidth)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self triggerTopItemRequestWith:(scrollView.contentOffset.x / self.itemWidth)];
}



- (void)updateScrollOffsetWith:(int)itemIndex {
    
    [self changeCollectionOffset:itemIndex];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self didChangeTagProperty:itemIndex];
    });
    
}

- (void)triggerTopItemRequestWith:(int)itemIndex {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemIndex inSection:0];
    
    if (self.didClickMenuTop) {
        self.didClickMenuTop(indexPath.item);
    }
    
}

- (void)changeCollectionOffset:(int)itemIndex {
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:itemIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

- (void)didChangeTagProperty:(int)itemIndex {
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:itemIndex inSection:0];
    
    if (self.currentIndexPath == indexPath) {
        return;
    }
    self.currentIndexPath = indexPath;
    
    [self.collectionView reloadData];
}


//点击菜单栏按钮
- (void)didClickMenuBtn {
    
    if (self.clickMenuColumn) {
        self.clickMenuColumn();
    }
    
}

@end
